package com.sdy.resdir.web.interceptor;

import com.sdy.auth.api.AuthApi;
import com.sdy.auth.api.model.UserDto;
import com.sdy.auth.client.service.SsoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@Component
@Slf4j
public class BizInterceptor extends HandlerInterceptorAdapter {
    @Value("${res.app.code}")
    private String appCode;
    @Autowired
    private SsoService ssoService;
    @Autowired
    private AuthApi authApi;
    
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        return super.preHandle(request, response, handler);
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        request.setCharacterEncoding("UTF-8");
    }
    
    private Map<String, Object> getUserInfo(HttpServletRequest request) {
        Integer userId = ssoService.getUserInfo(request).getUserId();
        UserDto user = authApi.userInfo(userId).getData();
        Map<String, Object> userInfo = new HashMap<>();
        userInfo.put("imgUrl", user.getImgUrl());
        userInfo.put("name", user.getName());
        return userInfo;
    }
}
